<?php
// @formatter:off
/**
 * @file WCParserBehaviorISSET.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-11 21:02:48 +0000 (Thu, 11 Jul 2013) $
 *
 * $Id: WCParserBehaviorISSET.php 95 2013-07-11 21:02:48Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/includes/parser/WCParserBehaviorISSET.php $
 */
// @formatter:on

//
// Loading father class.
require_once dirname(__FILE__)."/WCParserBehavior.php";

//
// Adding the function to evaluate by this behavior.
$wcParserBehaviorMatrix["ISSET"] = "WCParserBehaviorISSET";

/**
 * @class WCParserBehaviorISSET This is a specific behavior that checks the
 * existence of a variable from those set in the parser. When it's used it looks
 * like:
 * @code
 * <:ISSET:var=name:/>contents<::/>
 * @endcode
 */
class WCParserBehaviorISSET extends WCParserBehavior {
	/**
	 * @var string This is the type of warnings used when a IF condition is
	 * created but there's no variable to evaluate.
	 */
	const WarningNONAME = "NONAME_VARIABLE";
	/**
	 * This method perform a check of the object, sets the current status
	 * (variable $this->_ok) and return it.
	 *
	 * @return boolean Returns the current status after a check.
	 */
	public function check() {
		//
		// An ISSET behavior must belong to a parser node.
		if($this->_node) {
			//
			// An ISSET behavior requires one parameter at least
			if(count($this->_node->params()) < 1) {
				//
				// Behavior has no parameters. Error message is
				// set and the internal status becomes false.
				$this->_errorMsg = "ISSET without parameters";
				$this->_ok = false;
			} else {
				//
				// At this point, everything checks out.
				$this->_ok = true;
				$this->_errorMsg = "Ok";
			}
		} else {
			//
			// Behavior's node is not present. Error message is set
			// and the internal status becomes false.
			$this->_errorMsg = "ISSET node is not set";
			$this->_ok = false;
		}
		//
		// Returning current object status.
		return $this->isOk();
	}
	/**
	 * This method actually performs the variable existence check based on its
	 * node's data.
	 *
	 * @return string If the evaluated ISSET condition is true, it returns the
	 * nodes contents, and an empty string when false. Otherwise, returns
	 * false.
	 */
	public function get() {
		//
		// Setting the default value to be returned.
		$out = false;
		//
		// ISSET condition will only be evaluated when this behavior has
		// no issues.
		if($this->isOk()) {
			//
			// Loading parameters from the behavior's node.
			$params = $this->_node->params();
			//
			// Getting the name of the variable to be analyzed.
			$varname = isset($params["var"]) ? $params["var"] : false;
			//
			// Checking that there is a variable to evaluate.
			if(!$varname) {
				//
				// There's no variable name set, therefore, a
				// warning message is added.
				$this->_parser->setWarning(WCParserBehaviorISSET::WarningNONAME, "No variable set", false, false, __CLASS__);
			} else {
				//
				// Making the actual existence check.
				$isTrue = $this->_parser->has($varname);
				//
				// If there's a parameter '!' set, whatever the
				// current evaluation result is, it is toggled
				// with it opposite.
				if(isset($params["!"])) {
					$isTrue = !$isTrue;
				}
				//
				// Since node children may be nested behaviors,
				// they'll be returned parsed. But, only if the
				// condition was true.
				$out = $isTrue ? $this->_node->parseChildren() : "";
			}
		}
		//
		// Returning evaluation results.
		return $out;
	}
	/**
	 * This method redefines the inherited class initializer.
	 */
	protected function init() {
		//
		// Setting a type of warnings used when a IF condition is created
		// but there's no variable to evaluate.
		$this->_parser->setWarning(WCParserBehaviorISSET::WarningNONAME, false, false, "ISSET: no variable name. Use keyword 'var'");
	}
}
?>